package com.muyu.vehicle.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.domain.VehicleInfo;
import com.muyu.vehicle.mapper.VehicleInfoMapper;
import com.vehicle.common.core.utils.DateUtils;
import com.vehicle.common.core.utils.IdUtils;
import com.vehicle.common.redis.service.RedisService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.vehicle.mapper.VehicleMoveInfoMapper;
import com.muyu.vehicle.domain.VehicleMoveInfo;
import com.muyu.vehicle.service.IVehicleMoveInfoService;

/**
 * 车辆运行记录Service业务层处理
 *
 * @author muyu
 * @date 2021-09-04
 */
@Service
public class VehicleMoveInfoServiceImpl implements IVehicleMoveInfoService
{
    @Autowired
    private VehicleMoveInfoMapper vehicleMoveInfoMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;

    /**
     * 查询车辆运行记录
     *
     * @param id 车辆运行记录主键
     * @return 车辆运行记录
     */
    @Override
    public VehicleMoveInfo selectVehicleMoveInfoById(String id)
    {
        return vehicleMoveInfoMapper.selectVehicleMoveInfoById(id);
    }

    /**
     * 查询车辆运行记录列表
     *
     * @param vehicleMoveInfo 车辆运行记录
     * @return 车辆运行记录
     */
    @Override
    public List<VehicleMoveInfo> selectVehicleMoveInfoList(VehicleMoveInfo vehicleMoveInfo)
    {
        return vehicleMoveInfoMapper.selectVehicleMoveInfoList(vehicleMoveInfo);
    }

    /**
     * 新增车辆运行记录
     *
     * @param vehicleMoveInfo 车辆运行记录
     * @return 结果
     */
    @Override
    public int addVehicleMoveInfo(VehicleMoveInfo vehicleMoveInfo)
    {
        vehicleMoveInfo.setCreateTime(DateUtils.getNowDate());
        return vehicleMoveInfoMapper.insertVehicleMoveInfo(vehicleMoveInfo);
    }

    /**
     * 修改车辆运行记录
     *
     * @param vehicleMoveInfo 车辆运行记录
     * @return 结果
     */
    @Override
    public int updateVehicleMoveInfo(VehicleMoveInfo vehicleMoveInfo)
    {
        vehicleMoveInfo.setUpdateTime(DateUtils.getNowDate());
        return vehicleMoveInfoMapper.updateVehicleMoveInfo(vehicleMoveInfo);
    }

    /**
     * 批量删除车辆运行记录
     *
     * @param ids 需要删除的车辆运行记录主键
     * @return 结果
     */
    @Override
    public int deleteVehicleMoveInfoByIds(String[] ids)
    {
        return vehicleMoveInfoMapper.deleteVehicleMoveInfoByIds(ids);
    }

    /**
     * 删除车辆运行记录信息
     *
     * @param id 车辆运行记录主键
     * @return 结果
     */
    @Override
    public int deleteVehicleMoveInfoById(String id)
    {
        return vehicleMoveInfoMapper.deleteVehicleMoveInfoById(id);
    }

    /**
     * 车辆运行开始
     * @param vin
     */
    @Override
    public void addVehicleMoveInfo(String vin) {
        VehicleMoveInfo vehicleMoveInfo = new VehicleMoveInfo();
        vehicleMoveInfo.setId(IdUtils.fastSimpleUUID());
        vehicleMoveInfo.setStartTime(DateUtils.getNowDate());
        String vehicleId =vehicleInfoMapper.selectVehicleIDByVin(vin);
        vehicleMoveInfo.setVehicleId(vehicleId);
        vehicleMoveInfo.setVehicleVin(vin);
        redisService.setCacheObject(Config.VEHICLE_MOVE_INFO + vin , vehicleMoveInfo.getId());
        vehicleMoveInfoMapper.insertVehicleMoveInfo(vehicleMoveInfo);
    }

    /**
     * 车辆运行结束
     * @param vin
     */
    @Override
    public void vehicleMoveEndInfo(String vin) {
        String vehicleMoveId = redisService.getCacheObject(Config.VEHICLE_MOVE_INFO + vin);
        VehicleMoveInfo vehicleMoveInfo = new VehicleMoveInfo(){{
            setId(vehicleMoveId);
            setEndTime(DateUtils.getNowDate());
        }};
        vehicleMoveInfoMapper.updateVehicleMoveInfo(vehicleMoveInfo);
        redisService.deleteObject(Config.VEHICLE_MOVE_INFO + vin);

    }
}
